import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/state/refreshable_layout.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_rider/main.dart';
import 'package:flutter_rider/event/click_shop_event.dart';

class PopShopsList extends StatefulWidget {
  @override
  State createState() => _PopShopListState();
}

class _PopShopListState extends State<PopShopsList> {
  final _inputController = TextEditingController(text: "");
  final _stateController = RefreshableLayoutController();
  final List _result = [];
  var search_key ="";

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: MediaQuery.of(context).padding.left, right: MediaQuery.of(context).padding.right),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(8.rpx)),
      ),
      child: Column(
        children: [
          8.hspace,
          Container(
            width: 36.rpx,
            height: 5.rpx,
            decoration: BoxDecoration(
              color: Colors.black.withOpacity(0.26),
              borderRadius: BorderRadius.circular(2.rpx),
            ),
          ),
          Container(
            height: 48.rpx,
            alignment: Alignment.center,
            child: UText.get("选择店铺", Colors.black, 20.rpx, fw: FontWeight.bold),
          ),
          _renderInput(),
          16.hspace,
          Container(
            margin: EdgeInsets.only(left: 10.rpx, right: 10.rpx),
            height: 0.5.rpx,
            color: const Color(0xffE7E7E7),
          ),
          Expanded(
              child: RefreshableLayout(
                  controller: _stateController, urlCallback: _buildUrl, itemBuilder: _renderContent))
        ],
      ),
    );
  }

  String _buildUrl() {
    var baseUrl = "/waybill/ka_merchant";
    var searchKey = _inputController.text.trim();
    if (searchKey.isNotEmpty) {
      if (!baseUrl.contains("?")) {
        baseUrl += "?";
      } else {
        baseUrl += "&";
      }
      baseUrl += "search_key=${Uri.encodeComponent(searchKey)}";
    }
    return baseUrl;
  }

  Widget _renderContent(dynamic data, int index) {
    var item = data;
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: SizedBox(
        width: double.infinity,
        height: 64.rpx,
        child: Row(
          children: [
            16.wspace,
            SizedBox(
                width: 200.rpx,
                child: UText.get(item["name"], Colors.black, 16.rpx, fw: FontWeight.w600)
            ),
            UText.get(item["phone"], Colors.black, 16.rpx),
            24.wspace
          ],
        ),
      ),
      onTap: () {
        Navigator.of(context).pop();
        eventBus.fire(ClickShopEvent(item["name"],item["id"],item["phone"]));
      },
    );
  }

  Widget _renderInput() {
    return Container(
      color: UIColor.color_content,
      padding: EdgeInsets.symmetric(
        horizontal: 16.rpx,
      ),
      child:Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: TextField(
                controller: _inputController,
                textInputAction: TextInputAction.search,
                maxLength: 20,
                onEditingComplete: () {
                  _result.clear();
                  _stateController.reload();
                },
                decoration: InputDecoration(
                  constraints: BoxConstraints(minHeight: 44.rpx, maxHeight: 44.rpx),
                  counterText: "",
                  contentPadding: EdgeInsets.symmetric(vertical: 12.rpx, horizontal: 12.rpx),
                  isCollapsed: true,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(
                        2.rpx,
                      ),
                      borderSide: BorderSide.none),
                  prefixIcon: Padding(
                    padding: EdgeInsets.only(left: 12.rpx, right: 4.rpx),
                    child: Icon(
                      Icons.search,
                      size: 18.rpx,
                      color: Colors.black.withOpacity(0.4),
                    ),
                  ),
                  prefixIconConstraints: BoxConstraints(maxHeight: 16.rpx),
                  hintText: "请输入店铺名称或者手机号",
                  hintStyle: TextStyle(color: Colors.black.withOpacity(0.4)),
                  filled: true,
                  fillColor: UIColor.color_input,
                  suffixIconConstraints: BoxConstraints(maxHeight: 16.rpx),
                  suffixIcon: _inputController.text.isNotEmpty
                      ? Container(
                    padding: EdgeInsets.only(right: 6.rpx),
                    child: GestureDetector(
                      child: Icon(
                        Icons.close,
                        color: UIColor.color_t3,
                        size: 20.rpx,
                      ),
                      onTap: () {
                        _inputController.text = "";
                      },
                    ),
                  )
                  : null,
                ),
            ),
          ),
          8.wspace,
        ]
      )
    );
  }
}